package com.hoshiicloud.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.auth.vo.TokenUsersVO;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.user.entity.company.Member;
import com.hoshiicloud.user.entity.store.StoreUsers;
import com.hoshiicloud.user.param.PolymerizeStore;
import com.hoshiicloud.user.param.store.StoreUsersQueryParam;
import com.hoshiicloud.user.service.IStoreManageService;
import com.hoshiicloud.user.service.rbac.IMemberService;
import com.hoshiicloud.user.utils.UserInfoUtil;
import com.hoshiicloud.user.vo.store.StoreUsersVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 店铺管理后台聚合服务
 *
 * @author chenjie
 * @date 2019/8/9-13:45
 * @email rogercj1@126.com
 */

@Slf4j
@RestController
@RequestMapping("/storeManage")
@Api(value = "店铺管理后台", tags = {"店铺管理后台"})
public class StoreManageController {

    @Autowired
    private IStoreManageService iStoreManageService;

    @Autowired
    private IMemberService iMemberService;

    @ApiOperation("创建店铺聚合服务")
    @RequestMapping(value = "/createStore", method = RequestMethod.POST)
    public Response createStore(@Valid @RequestBody PolymerizeStore polymerizeStore,@ApiIgnore HttpServletRequest request) {
        Boolean flag =  false;
        TokenUsersVO user = UserInfoUtil.getUserInfo(request);
        polymerizeStore.setPlatId(user.getPlatId());
        Member member = new Member();
        member = iMemberService.getMemberByMobile(user.getCompanyId(),user.getUsername());
        if(member!=null){
            polymerizeStore.setMemberId(member.getId());
        }
        //polymerizeStore.setStoreId(user.getStoreId());
        flag = iStoreManageService.createStore(polymerizeStore);
        return Response.data(flag);
    }


    @ApiOperation("店铺管理-->员工列表")
    @RequestMapping(value = "/storeUserPageList", method = RequestMethod.POST)
    public Response<IPage<StoreUsers>> storeUserPageList(@Valid @RequestBody (required = false) StoreUsersQueryParam param, HttpServletRequest request) {
        TokenUsersVO user = UserInfoUtil.getUserInfo(request);
        if(param==null){
            param = new StoreUsersQueryParam();
        }
        if(user.getStoreId()!=null && !user.getStoreId().equals("")){
            param.setStoreId(user.getStoreId());
        }
        IPage<StoreUsersVO> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
        //String key = param.toString()+ ",Page{pageNumber="+param.getPageNumber()+",pageSize="+param.getPageSize()+"}";
        return iStoreManageService.storeUserPageList( page,param, param.getStoreId(),user.getCompanyId());
    }

    @ApiOperation("店铺管理-->角色列表")
    @RequestMapping(value = "/getRoleList", method = RequestMethod.POST)
    public Response<IPage<StoreUsersVO>> getRoleList(@Valid @RequestBody StoreUsersQueryParam param,HttpServletRequest request) {
        IPage<StoreUsersVO> page = new Page<>(Long.valueOf(param.getPageNumber()), Long.valueOf(param.getPageSize()));
        String key = param.toString()+ ",Page{pageNumber="+param.getPageNumber()+",pageSize="+param.getPageSize()+"}";
        TokenUsersVO user = UserInfoUtil.getUserInfo(request);
        if(user.getStoreId()!=null && !user.getStoreId().equals("")){
            param.setStoreId(user.getStoreId());
        }
        return iStoreManageService.getRoleList(page,param,key);
    }

}
